/*
 * Copyright (c) 2006-2021, Ulandlink Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-02-20     xpw          sync
 */
#ifndef __HTTP_PARSE_H__
#define __HTTP_PARSE_H__

#include <stdlib.h>

#include <dfs_posix.h>
#include <rtthread.h>
#include <ulog.h>

#if HTTP_SERVER_ENABLE

#define HTTP_REQUEST_URL_MAXLEN         128
#define HTTP_RESPONSE_HEADER_MAXLEN     256
#define HTTP_POST_CONTENT_MAXLEN        512
#define HTTP_FILE_CHUNK_BUFFER          4096

enum http_method_e
{
    HTTP_METHOD_UNKNOWN = 0,
    HTTP_METHOD_GET,
    HTTP_METHOD_POST,
    
    HTTP_METHOD_MAX,
};

struct http_method
{
    const char *method_str;
    enum http_method_e http_method_e;
};

extern const struct http_method http_method_map[];

enum http_protocol
{
    HTTP_PROTOCOL_UNKNOWN = 0,    
    /* http/1.0 */
    HTTP_PROTOCOL_0,    
    /* http/1.1 */
    HTTP_PROTOCOL_1,
};

enum http_connection
{
    HTTP_CONN_UNKNOWN = 0,
    HTTP_CONN_CLOSED,
    HTTP_CONN_ALIVE,
};

struct param_s
{
    char *key;
    char *value;
    struct param_s *next;
};

typedef struct param_s param_t;

struct param_list_s
{
    param_t *head;
    int size;
};

typedef struct param_list_s param_list_t;

typedef struct http_request_s
{
    enum http_method_e method;
    enum http_protocol protocol;
    char *url;
    int result_code;
    char *host;
    enum http_connection connection;
    int content_length;
    char *content_type;
    char *authorization;
    rt_bool_t support_gzip;
    char *user_agent;
    char *accept;
    char *accept_encoding;
    char *accept_language;
    char *content_buffer;
    char *cookie;
    char *referer;
    param_list_t *param_list;
} http_request_t;

typedef struct http_session_s
{
    int socket;
    http_request_t *request;
} http_session_t;

char *http_get_result_str(int result_code);
void http_request_destroy(http_request_t **request);
void http_content_destroy(http_request_t **request);
void http_resp_request(int socket, http_request_t *request);
void http_print_request(int socket, http_request_t *request);
rt_bool_t http_recv_request(int socket, http_request_t *request);
rt_bool_t http_parse_header(char* http_header_buffer, http_request_t *request);
void http_send_url_header(int socket, http_request_t *request, char *send_url);

#endif
#endif
